package bmcsoft.webservicelearning;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class Exercise30_13Server extends JFrame {
    private JTextArea jta = new JTextArea();

    public static void main(String[] args) {
        new Exercise30_13Server();
    }

    public Exercise30_13Server(){
        setLayout(new BorderLayout());
        add(new JScrollPane(jta),BorderLayout.CENTER);

        setTitle("Exercise30_13Server");
        setSize(500,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        try {
            ServerSocket serverSocket = new ServerSocket(8080);
            jta.append("Server start at " + new Date() + '\n');

            Socket socket = serverSocket.accept();

            DataInputStream inputFromClient = new DataInputStream(
                    socket.getInputStream()
            );
            DataOutputStream outputToClient = new DataOutputStream(
                    socket.getOutputStream()
            );
            while (true){
                String text = inputFromClient.readUTF();
                String area = new Date() + "\n" + text;

                outputToClient.writeUTF(area);

                jta.append(area + '\n');
            }
        }catch (IOException ex){
            System.err.println(ex);
        }
    }
}
